package main

import (
	"aigen-todo/internal/config"
	"aigen-todo/internal/database"
	"aigen-todo/internal/models"
	"fmt"
	"log"
	"strings"
)

func main() {
	// 加载配置
	cfg := config.LoadConfig()

	// 连接数据库
	db, err := database.Connect(cfg.Database.URL, nil)
	if err != nil {
		log.Fatalf("Failed to connect to database: %v", err)
	}

	// 自动迁移数据库表
	models := []interface{}{
		&models.User{},
		&models.TaskGroup{},
		&models.Task{},
		&models.TaskLog{},
	}

	for _, model := range models {
		err = db.AutoMigrate(model)
		if err != nil {
			// 忽略特定的约束不存在错误
			if strings.Contains(err.Error(), "约束") && strings.Contains(err.Error(), "不存在") {
				fmt.Printf("Ignoring constraint error for %T: %v\n", model, err)
				continue
			}
			log.Fatalf("Failed to migrate %T: %v", model, err)
		}
		fmt.Printf("Successfully migrated %T\n", model)
	}

	log.Println("Database migration completed successfully")
}
